package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.WebServerOrderSaveVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务预约订单Controller
 * 
 * @author wsliupan@126.com
 * @date 2021-06-23
 */
@Controller
@RequestMapping("/system/serverOrder")
public class WebServerOrderController extends BaseController
{
    private String prefix = "system/serverOrder";
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private IWebServerOrderService webServerOrderService;
    @Autowired
    private IWebServerUserService iWebServerUserService;
    @Autowired
    private IWebUserService iWebUserService;

    @RequiresPermissions("system:serverOrder:view")
    @GetMapping()
    public String serverOrder()
    {
        return prefix + "/serverOrder";
    }

    /**
     * 查询服务预约订单列表
     */
    @RequiresPermissions("system:serverOrder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebServerOrder webServerOrder)
    {
        startPage();
        List<WebServerOrder> list = webServerOrderService.selectWebServerOrderList(webServerOrder);
        return getDataTable(list);
    }

    /**
     * 导出服务预约订单列表
     */
    @RequiresPermissions("system:serverOrder:export")
    @Log(title = "服务预约订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebServerOrder webServerOrder)
    {
        List<WebServerOrder> list = webServerOrderService.selectWebServerOrderList(webServerOrder);
        ExcelUtil<WebServerOrder> util = new ExcelUtil<WebServerOrder>(WebServerOrder.class);
        return util.exportExcel(list, "服务预约订单数据");
    }

    /**
     * 新增服务预约订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存服务预约订单
     */
    @RequiresPermissions("system:serverOrder:add")
    @Log(title = "服务预约订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebServerOrder webServerOrder)
    {
        return toAjax(webServerOrderService.insertWebServerOrder(webServerOrder));
    }

    /**
     * 修改服务预约订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap,String isConfirm)
    {
        WebServerOrder webServerOrder = null;
        if(id == null || id ==  -1){
            webServerOrder = new WebServerOrder();
            mmap.put("listServerTime","");
        }else{
            webServerOrder = webServerOrderService.selectWebServerOrderById(id);
            SysDept dept = iSysDeptService.selectDeptById(webServerOrder.getDeptId());
            mmap.put("listServerTime",dept.getTimes().split(","));

            //查询服务技师
            List<WebServerUser> listServerUser =iWebServerUserService.list(new QueryWrapper<WebServerUser>()
                    .eq("dept_id",webServerOrder.getDeptId())
                    .eq("status",0)
                    .select("realname","id")
            );
            mmap.put("listServerUser",listServerUser);
        }
        WebUser user = new WebUser();
        List<WebUser> users = iWebUserService.selectWebUserList(user);
        mmap.put("webServerOrder", webServerOrder);
        mmap.put("users", JSON.toJSONString(users));
        WebRecycleType webRecycleType = new WebRecycleType();
        webRecycleType.setType(1);
        webRecycleType.setParentId(0L);
        List<WebRecycleType> listRecycleType = iwebRecycleTypeService.selectWebRecycleTypeList(webRecycleType);
        mmap.put("listRecycleType",listRecycleType);
        if("1".equals(isConfirm)){
            //查询服务技师
            List<WebServerUser> listServerUser =iWebServerUserService.list(new QueryWrapper<WebServerUser>()
                    .eq("dept_id",webServerOrder.getDeptId())
                    .eq("status",0)
                    .select("realname","id")
            );
            mmap.put("listServerUser",listServerUser);

            //查询服务流程
            if(webServerOrder.getRecycleTypeId() != null){
                List<WebRecycleType> webRecycleTypeList = iwebRecycleTypeService.list(new QueryWrapper<WebRecycleType>()
                        .eq("parent_id",webServerOrder.getRecycleTypeId())
                        .eq("status",0)
                        .eq("type",1)
                        .select("name","id")
                );

                //查询可以添加的服务流程
                List<WebRecycleType> webRecycleTypeList2 = iwebRecycleTypeService.list(new QueryWrapper<WebRecycleType>()
                      //  .eq("parent_id",webServerOrder.getRecycleTypeId())
                        .eq("status",0)
                        //.eq("type",2)
                        .select("name","id")
                );
                mmap.put("webRecycleTypeList",webRecycleTypeList);
                mmap.put("webRecycleTypeList2",webRecycleTypeList2);
            }
            if(webServerOrder.getStatus() ==2){
                List<WebServerOrderFlow> listFlows = iWebServerOrderFlowService.list(new QueryWrapper<WebServerOrderFlow>().eq("order_id",webServerOrder.getId()));
                mmap.put("listFlows",listFlows);

            }



            return prefix + "/confirm";
        }
        return prefix + "/edit";
    }

    /**
     * 修改保存服务预约订单
     */
    @RequiresPermissions("system:serverOrder:edit")
    @Log(title = "服务预约订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebServerOrder webServerOrder)
    {
        return toAjax(webServerOrderService.updateWebServerOrder(webServerOrder));
    }


    /**
     * 修改保存服务预约订单
     */
    @RequiresPermissions("system:serverOrder:edit")
    @Log(title = "确认服务流程", businessType = BusinessType.UPDATE)
    @PostMapping("/confirmSave")
    @ResponseBody
    public AjaxResult confirmSave(WebServerOrderSaveVO orderSaveVO)
    {
        this.webServerOrderService.confirmSave(orderSaveVO);
        return success();
    }

    /**
     * 删除服务预约订单
     */
    @RequiresPermissions("system:serverOrder:remove")
    @Log(title = "服务预约订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(webServerOrderService.deleteWebServerOrderByIds(ids));
    }


    /**
     * Ajax查询技师
     * @param deptId
     * @return
     */
    @GetMapping( "/getServerListByAjax")
    @ResponseBody
    public AjaxResult getServerListByAjax(Long deptId){
        List<WebServerUser> listServerUser =iWebServerUserService.list(new QueryWrapper<WebServerUser>()
                .eq("dept_id",deptId)
                .eq("status",0)
                .select("realname","id")
        );

        return AjaxResult.success(listServerUser);
    }


    @Autowired
    private IWebRecycleTypeService iwebRecycleTypeService;
    @Autowired
    ISysDeptService iSysDeptService;
    @Autowired
    private IWebServerOrderFlowService iWebServerOrderFlowService;
}
